package com.csit.service.impl;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.csit.bean.Pager;
import com.csit.dao.ProductDao;
import com.csit.dao.WastageDao;
import com.csit.entity.Product;
import com.csit.entity.Wastage;
import com.csit.service.WastageService;
import com.csit.util.ListAndJsonUtil;
import com.csit.util.ResponseMsgUtil;
import com.csit.util.StringUtil;

/**
 * 
 * @Description: 损耗service的实现类
 * @Copyright: 福州骏华信息有限公司 (c)2012
 * @Created Date : 2012-12-8
 * @author longweier
 * @vesion 1.0
 */
@Service
public class WastageServiceImpl extends BaseServiceImpl<Wastage, Integer> implements WastageService {

	@Resource
	private WastageDao wastageDao;
	@Resource
	private ProductDao productDao;
	@Resource
	public void setBaseDao(WastageDao wastageDao) {
		super.setBaseDao(wastageDao);
	}
	
	public ResponseMsgUtil addWastage(Wastage wastage, String productids,String qtys, String prices, String notes) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(StringUtil.isNull(productids)){
			msgUtil.setMsg("至少输入一种商品");
			return msgUtil;
		}
		String[] productidArray = productids.split(SPLIT_REGEX);
		String[] qtyArray = qtys.split(SPLIT_REGEX);
		String[] priceArray = prices.split(SPLIT_REGEX);
		String[] noteArray = notes.split(SPLIT_REGEX);
		for(int i=0;i<productidArray.length;i++){
			Product product = productDao.get(Integer.parseInt(productidArray[i]));
			
			Wastage model = new Wastage();
			model.setShop(wastage.getShop());
			model.setOperater(wastage.getOperater());
			model.setProduct(product);
			model.setCreateDate(new Date());
			model.setQuantity(Double.parseDouble(qtyArray[i]));
			model.setPrice(Double.parseDouble(priceArray[i]));
			model.setAmount(model.getQuantity()*model.getPrice());
			if(!"*".equals(noteArray[i])){
				model.setNote(noteArray[i].trim());
			}
			//更新库存
			product.setCurrentQuantity(product.getCurrentQuantity()-model.getQuantity());
			saveEntity(model);
		}
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public String query(Integer pageNumber, Integer pageSize, Wastage wastage,Date beginDate, Date endDate) {
		Pager pager = new Pager(pageNumber, pageSize);
		pager = wastageDao.queryWastageList(pager, wastage, beginDate, endDate);
		String[] propertiesName ={"wastageID","quantity","price","amount","operater","createDate","note",
					   "product.productName","product.productSize","product.unitName"};
		String jsonArray = ListAndJsonUtil.getJSONString(pager.getList(), propertiesName, pager.getTotalCount());
		return jsonArray;
	}

	public ResponseMsgUtil updateWastage(Wastage wastage) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		if(wastage.getQuantity()==null){
			msgUtil.setMsg("数量不能为空");
			return msgUtil;
		}
		if(wastage.getPrice()==null){
			msgUtil.setMsg("价格不能为空");
			return msgUtil;
		}
		Wastage model = getEntity(wastage.getWastageID());
		model.setQuantity(wastage.getQuantity());
		model.setPrice(wastage.getPrice());
		model.setAmount(wastage.getQuantity()*wastage.getPrice());
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public ResponseMsgUtil deleteWastage(Wastage wastage) {
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		Wastage model = getEntity(wastage.getWastageID());
		Product product = model.getProduct();
		//更新库存
		product.setCurrentQuantity(product.getCurrentQuantity()+model.getQuantity());
		deleteEntity(model);
		msgUtil.setSuccess(true);
		return msgUtil;
	}

	public Map<String, Double> countWastage(Wastage wastage, Date beginDate,Date endDate) {
		return wastageDao.queryTotal(wastage, beginDate, endDate);
	}

}
